#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from django.db import models
from tinymce.models import HTMLField
import django.utils.timezone as timezone
import re


def get_content_preview(content):
    pattern = re.compile(r'<[^>]+>', re.S)
    result = pattern.sub('', content)
    return result[:256]


class SchoolArticle(models.Model):
    article_title = models.CharField(verbose_name="标题", max_length=512, default=None, db_column='article_title')
    article_type = models.CharField(verbose_name="类型", max_length=64, default=None,
                                    db_column='article_type', editable=False)
    article_preview = models.CharField(verbose_name="内容预览", max_length=512, default=None, db_column='article_preview',
                                       editable=False)
    article_date = models.DateTimeField('创建时间', default=timezone.now, editable=False)
    article_content = HTMLField(verbose_name="内容")


class SchoolOrganizationInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '学校概况-组织机构'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '组织机构'
        self.article_preview = get_content_preview(self.article_content)
        super(SchoolOrganizationInfo, self).save(*args, **kwargs)


class SchoolHonorInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '学校概况-学校荣誉'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '学校荣誉'
        self.article_preview = get_content_preview(self.article_content)
        super(SchoolHonorInfo, self).save(*args, **kwargs)


class SchoolContactInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '学校概况-联系我们'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '联系我们'
        self.article_preview = get_content_preview(self.article_content)
        super(SchoolContactInfo, self).save(*args, **kwargs)


class PartyStaffInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '党建工作-教职工党建'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '教职工党建'
        self.article_preview = get_content_preview(self.article_content)
        super(PartyStaffInfo, self).save(*args, **kwargs)


class PartyTeacherInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '党建工作-师生党建'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '师生党建'
        self.article_preview = get_content_preview(self.article_content)
        super(PartyTeacherInfo, self).save(*args, **kwargs)


class NewsCenterInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '公告中心-校内新闻'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '校内新闻'
        self.article_preview = get_content_preview(self.article_content)
        super(NewsCenterInfo, self).save(*args, **kwargs)


class AnnounceCenterInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '公告中心-学校公示'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '学校公示'
        self.article_preview = get_content_preview(self.article_content)
        super(AnnounceCenterInfo, self).save(*args, **kwargs)


class MenuCenterInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '公告中心-每日菜谱'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '每日菜谱'
        self.article_preview = get_content_preview(self.article_content)
        super(MenuCenterInfo, self).save(*args, **kwargs)


class BeautifulBlackboardInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '精彩校园-黑板报'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '黑板报'
        self.article_preview = get_content_preview(self.article_content)
        super(BeautifulBlackboardInfo, self).save(*args, **kwargs)


class BeautifulSportInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '精彩校园-运动会'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '运动会'
        self.article_preview = get_content_preview(self.article_content)
        super(BeautifulSportInfo, self).save(*args, **kwargs)


class BeautifulActivityInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '精彩校园-集体活动'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '集体活动'
        self.article_preview = get_content_preview(self.article_content)
        super(BeautifulActivityInfo, self).save(*args, **kwargs)


class HomeQuestionnaireInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '家校互动-问卷调查'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '问卷调查'
        self.article_preview = get_content_preview(self.article_content)
        super(HomeQuestionnaireInfo, self).save(*args, **kwargs)


class HomeFeedbackInfo(SchoolArticle):
    class Meta:
        proxy = True
        verbose_name = '家校互动-意见反馈'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):
        self.article_type = '意见反馈'
        self.article_preview = get_content_preview(self.article_content)
        super(HomeFeedbackInfo, self).save(*args, **kwargs)

